#define MAXSIZE 100
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;

typedef enum{
  LEFT_PARE,RIGHT_PARE,
  ADD,SUB,MUL,DIV,MOD,
  EOS,//end of string
  NUM
}contentType;


typedef struct {
  ElemType *data;
  int top;
}Stack;

//初始化
Stack* initStack(){
  Stack *s=(Stack*)malloc(sizeof(Stack));
  s->data=(ElemType*)malloc(sizeof(ElemType)*MAXSIZE);
  s->top=-1;
  return s;
}

//判断是否为空
int isEmpty(Stack* s){
  if(s->top==-1){ 
    printf("栈为空\n");
    return 1;
  }
  else return 0;
}

//压栈
int push(Stack* s,ElemType e){
  if(s->top>=MAXSIZE-1){
    printf("栈已满\n");
    return 0;
  }
  s->top++;
  s->data[s->top]=e;
  return 1;
}

//出栈
int pop(Stack* s,ElemType *e){
  if(isEmpty(s)==1) return 0;
  //存储出栈元素
  *e=s->data[s->top];
  s->top--;
  return 1;
}

//获取栈顶元素
int getTop(Stack* s,ElemType *e){
  if(isEmpty(s)==1) return 0;
  *e=s->data[s->top];
  return 1;
}

char expr[]="82/2+56*-";

contentType getToken(char *symbol,int *index){
  *symbol=expr[*index];
  *index=*index+1;
  switch (*symbol){
  case '(':
    return LEFT_PARE;
  case ')':
    return RIGHT_PARE;
  case '+':
    return ADD;
  case '-':
    return SUB;
  case '*':
    return MUL;
  case '/':
    return DIV;
  case '%':
    return MOD;
  case '\0':
    return EOS;
  

  default:
    return NUM;
  }
}

int evaluate(Stack *s){
  char symbol;
  int o1,o2;
  int index=0;
  contentType token;
  token=getToken(&symbol,&index);
  ElemType res;
  while(token!=EOS){
    if(token==NUM){
      push(s,symbol-'0');
    }else{
      pop(s,&o2);
      pop(s,&o1);
      switch(token){
        case ADD:
          push(s,o1+o2);
          break;
        case SUB:
          push(s,o1-o2);
          break;
        case MUL:
          push(s,o1*o2);
          break;
        case DIV:
          push(s,o1/o2);
          break;
        case MOD:
          push(s,o1%o2);
          break;
        
        default:
          break;
      }
    }
    token=getToken(&symbol,&index);
  }
  pop(s,&res);
  printf("%d\n",res);
  return 1;
}

int main(){
  Stack *s = initStack();
	
  evaluate(s);
	
	return 0;
}

